GBML manual / Maths

wrap

Steps value off one end of [min, max] and reappears at the other - a menu index looping from the last row back to the first, a small cycling counter. NOT a modulo wrap - see the "On the GBA" note before reaching for this the way you would % elsewhere.

Syntax

wrap(value, min, max) -> real

Arguments

NameType
valuereal
minreal
maxreal

Returns

real

Description

Steps value off one end of [min, max] and reappears at the other. Built for the "next/previous item in a small loop of values" shape - a menu index that goes from the last row back to the first, a small cycling counter, any value that should never actually leave its own range.

This is not a modulo wrap. A value far past max lands on min in one step, not on min + (value - min) % (max - min). If you have used % for wraparound in another language, assume nothing - read the "On the GBA" section below before reaching for this the way you would % there.

wrap hands back a real, even when every number is whole. A menu row is a whole number, so for that - and for any counter that loops - use wrap_int, which keeps the answer a whole number.

Example

var row_count = 4;
var next_row = wrap(2 + 1, 0, row_count - 1);

Moves a menu cursor from row 2 to row 3 - and if it had already been on the last row (row_count - 1), the identical call would have looped it straight back to row 0 instead of counting past the end. A real menu keeps selected_row as a project-wide value (updated the same way, selected_row = wrap(selected_row + 1, 0, row_count - 1);, typically inside if (btn_pressed(BTN_DOWN))) rather than a fresh local every frame - this example inlines the numbers just to keep it self-contained.

Cost

value already inside [min, max] - by far the common case, since a cursor only ever moves one step at a time - is returned completely unchanged: a couple of comparisons, as cheap as clamp.

wrap(200, 0, 9) is 0, not the 2 a modulo wrap would give - a value that jumps several widths past the end still lands on min in one step, because that is what "the next slot after the last one" actually means for a small loop of values. Reach for a real modulo (write your own, e.g. value - int_floor(value / range) * range) instead whenever the thing wrapping is a position that may have scrolled several widths past a looping edge - wrap never adds anything on its own; a caller already adds or subtracts its own step before calling this.

min/max may arrive in either order - a reversed pair is swapped first, the same posture clamp already takes for the identical argument shape.

move_wrap is this same rule applied to an instance's own x/y against a rectangle, with an optional margin a position wrap can use but a plain value wrap has no reason to - reach for move_wrap when the thing wrapping is self's own position, and wrap for any other number.

On the GBA

value below min returns max, and value above max returns min, in ONE step, however far past the end value actually is - wrap(200, 0, 9) is 0, not the 2 a modulo wrap (min + (value - max) % (max - min)) would give. Right for "the next/previous item in a small loop of values" (add or subtract your own step before calling this); wrong for a position that scrolled several widths past a looping edge, which wants a real modulo instead. min/max may arrive either order - a reversed pair is swapped first, the same posture clamp takes. It hands back a real, even when every number is whole: for a menu row or a counter that loops, wrap_int keeps the answer a whole number.

See also: move_wrapclampwavewrap_int